<?php
namespace Mobile\Controller;

use Common\Api\Wechat;
use Think\Controller;

class ClientServerController extends Controller
{
    protected $wap_userId;
    protected $wObj;
    protected $open_id;
    protected $logoImg;

    public function _initialize()
    {
         $user= $_SERVER['HTTP_USER_AGENT'];
         if(!strpos($user,"icroMessenger")) {
             $this->error('请在微信浏览器中使用');
         }
        $wap_userId = cookie('wap_userId');//用户id
        if (empty($wap_userId)) {
            $this->error('请先登录', U('Wap/login'));
            exit;
        } else {
            $this->wap_userId = $wap_userId;
        }

        $re = M('WechatConfig')->where("wid=%d", 1)->find();
        //获取配置数据
        $config = array(

            'appid' => $re['appid'], //填写高级调用功能的appid

            'appsecret' => $re['appsecret'], //填写高级调用功能的密钥

            'wechat_id' => $re['wid']//微信id
        );

        $this->open_id = cookie('wap_openid');
        $this->config = $config;
        $this->logoImg=$re['logoimg'];
        $this->wObj = new Wechat($this->config);


        if ($_GET['stateid']) {
            $this->assign('stateid', $_GET['stateid']);
        }

    }

    //我要下单
    public function placeAnOrder()
    {
        $userInfo = M('DealerUserinfo')->field("a.*,b.area_name")
            ->alias("a")->join("sr_area as b on a.belong_to_area=b.id")->where("a.id=%d and a.status=%d", [$this->wap_userId, 1])->find();

        if (!$userInfo) {
            $this->error('账号被禁用');
        } else {
            $this->assign('userInfo', $userInfo);
            $this->assign('title', '我要下单');
            $this->display();
        }

    }

    //上传订单图片
    public function orderCommit($file = '')
    {
        $upload = new \Think\Upload();
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '/images/'; // 设置附件上传（子）目录
        $info = $upload->upload();
        if (!$info) {
            $this->ajaxReturn($upload->getError());
        } else {

            $up_img = $info['file'];
            $img = '/Uploads/' . substr($up_img['savepath'], 1) . $up_img['savename'];
            $arr['state'] = 'success';
            $arr['info'] = "http://" . $_SERVER['HTTP_HOST'] . $img;
            $arr['url'] = $img;
            $this->ajaxReturn($arr);
        }
    }

    //提交订单
    public function getOrder()
    {
        if (IS_POST) {
            $user_id = $_POST['user_id'];//客户id
            $useInfo = M('DealerUserinfo')->where("id=%d", $user_id)->find();
            $_POST['order_number'] = getordcode();
            $_POST['username'] = $useInfo['username'];
            $_POST['company_address'] = $useInfo['company_address'];
            $_POST['mobile_phone'] = $useInfo['mobile_phone'];
            $_POST['order_time'] = time();
            $result = D('Orders')->orderAdd($_POST);//写入订单
            if ($result['state'] == 'success') {

                $data['url'] = U('Admin/Orders/stayOrderList');
                $data['rid'] = $result['rid'];
                $data['sr_time'] = time();
                $data['content'] = "【新订单提醒】订单号：" . $_POST['order_number'] . "请尽快查看处理!";
                D('Inform')->add($data);

                $templateData = [
                    'touser' => $this->open_id,
                    'template_id' => Wechat::TEMPLATE_NEW,
                    'url' => 'http://' . $_SERVER['HTTP_HOST'] . '/Mobile/ClientServer/personageCenter',
                    'topcolor' => Wechat::TEMPLATE_COLOR_THEME,
                    'data' => [
                        'first' =>
                            ['value' => '订单提交成功~待处理', 'color' => Wechat::TEMPLATE_COLOR_TEXT],
                        'keyword1' =>
                            ['value' => $_POST['order_number'], 'color' => Wechat::TEMPLATE_COLOR_TEXT],
                        'keyword2' =>
                            ['value' => date('Y-m-d H:i:s', $_POST['order_time']), 'color' => Wechat::TEMPLATE_COLOR_TEXT],
                        'remark'=>['value'=>'提交订单成功,我们将尽快帮您处理，请耐心等待','color'=>Wechat::TEMPLATE_COLOR_TEXT]
                    ]

                ];

                $this->wObj->sendTemplateMessage($templateData);
                $this->success($result['info'], U('ClientServer/placeAnOrder'));
            } else {
                $this->error($result['info']);
            }


        }
    }

    //订单查询
    public function orderInquire()
    {
        $result = D('Orders')->myOrderList($this->wap_userId);
        $this->assign('_list', $result['list']);
        $this->assign('_page', $result['page']->show());
        $this->assign('title', '我的订单');
        $this->display();
    }

    //订单状态
    public function orderDetail()
    {

        $rid = $_GET['rid'];
        $list = M('Tailafter')->where("rid=%d and status=%d", [$rid, 1])->order("tid desc")->select();
        $order = M('Orders')->field("rid,order_number,status")->where("rid=%d", $rid)->find();
        $this->assign('order', $order);
        $this->assign('title', '订单状态');
        $this->assign('_list', $list);

        $this->assign('logoImg',$this->logoImg);
        $this->display();
    }

    //个人中心
    public function personageCenter()
    {
        $result = M('DealerUserinfo')->field("a.*,b.area_name")->alias("a")->join("sr_area as b on a.belong_to_area=b.id")
            ->where("a.id=%d", $this->wap_userId)->find();

        $this->assign('result', $result);
        $this->assign('title', '个人中心');
        $this->display();
    }

    //订单详情信息
    public function orderBaseDetail()
    {

        $rid = $_GET['rid'];
        $data = M('Orders')->where("rid=%d", $rid)->find();
        $this->assign('data', $data);
        $this->assign('rid', $rid);
        $this->display();
    }

    //商品详情
    public function productDetail()
    {
        $rid = $_GET['rid'];
        $pdata = M('Products')->where("rid=%d", $rid)->select();

        $this->assign('data', $pdata);
        $this->assign('rid', $rid);
        $this->display();
    }

    //工艺详情
    public function craftDetail()
    {
        $rid = $_GET['rid'];
        $data = M('process')->where("rid=%d", $rid)->select();
        $this->assign('data', $data);
        $this->assign('rid', $rid);
        $this->display();
    }
    //搜索订单
    public function searchOrder()
    {
        $keywords=$_POST['keywords'];

        if($keywords){
            $where['orderNumber']=array('like'=>"%$keywords%");
            $where['user_id']=$this->wap_userId;
//            $where['status']=1;
            $list= M('Orders')->where($where)->order("rid desc")->select();
            $this->assign('_list',$list);
            $this->display('orderInquire');
        }
    }


}














